/*
 * Copyright (c) Hisilicon Technologies Co., Ltd. 2019-2019. All rights reserved.
 * Description: This file is used for adas calibrate
 * Author:  HiMobileCam Reference Develop Team
 * Create: 2019/11/08
 */


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "hi_mapi_vproc.h"

#include "ui_common.h"


#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif /*  __cplusplus  */


typedef struct {
    HI_BOOL calibStatus;
}UI_ADASCalibCtx;

UI_ADASCalibCtx g_adasCalibCtx = {.calibStatus = HI_FALSE};

static HI_S32 PDT_UI_ADASCalibrateExit(HI_VOID)
{
    HI_S32 ret = PDT_UI_WINMNG_FinishWindow(ADAS_CALIBRATE_WINDOW);
    HI_APPCOMM_CHECK_RETURN(ret, HI_FAILURE);
    ret = PDT_UI_ADASPageExit();
    HI_APPCOMM_CHECK_RETURN(ret, HIGV_PROC_GOON);
    ret = PDT_UI_SetPageExit();
    HI_APPCOMM_CHECK_RETURN(ret, HIGV_PROC_GOON);

    return HI_SUCCESS;
}

static HI_S32 PDT_UI_AdasCalibOnReceiveMsgResult(HI_EVENT_S* event)
{
    HI_S32 ret = PDT_UI_WINMNG_FinishWindow(BUSY_PAGE);
    HI_APPCOMM_CHECK_RETURN(ret, ret);
    ret = PDT_UI_ADASCalibrateExit();
    HI_APPCOMM_CHECK_RETURN(ret, HIGV_PROC_GOON);
    g_adasCalibCtx.calibStatus = HI_TRUE;
    return HI_SUCCESS;
}

static HI_S32 PDT_UI_SendADASCalibMsg(HI_VOID)
{
    HI_U32 vanishX, vanishY;
    HI_PDT_AdasPoint vanishAttr;
    HI_PDT_AdasPoint algoAttr;
    HI_PDT_STATEMNG_WORKMODE_STATE_S workState;

    HI_S32 camID = HI_PDT_STATEMNG_GetPreViewCamID();
    HI_S32 ret = HI_PDT_STATEMNG_GetState(&workState);
    HI_APPCOMM_CHECK_RETURN(ret, ret);
    ret = HI_PDT_PARAM_GetCamParam(workState.enWorkMode, camID, HI_PDT_PARAM_TYPE_ADAS_VANISH, &vanishAttr);
    HI_APPCOMM_CHECK_RETURN(ret, ret);

    if(vanishAttr.x == 0 || vanishAttr.y == 0) {
        HI_PDT_MEDIA_BIND_SRC_CFG_S dispCfg;
        HI_MAPI_VPORT_ATTR_S vPortAttr = {0};

        HI_PDT_PARAM_GetDispSrcByCamID(camID,&dispCfg);
        ret = HI_MAPI_VPROC_GetPortAttr(dispCfg.ModHdl, dispCfg.ChnHdl, &vPortAttr);
        HI_APPCOMM_CHECK_RETURN(ret, HI_FAILURE);
        ret = HI_PDT_PARAM_GetCamParam(workState.enWorkMode, camID, HI_PDT_PARAM_TYPE_ADAS_ALGOATTR, &algoAttr);
        HI_APPCOMM_CHECK_RETURN(ret, ret);
        if(algoAttr.x == 0 || algoAttr.y == 0) {
            MLOGW("can not get alg src resulotion!\n");
            return HI_FAILURE;
        }
        vanishAttr.x = vPortAttr.u32Width / 2U;
        vanishAttr.y = vPortAttr.u32Height / 2U + 50U;
        vanishX = vanishAttr.x * algoAttr.x / vPortAttr.u32Width;
        vanishY = vanishAttr.y * algoAttr.y / vPortAttr.u32Height;
        MLOGD("point:[%d][%d],vanish:[%d][%d]\n",vanishAttr.x,vanishAttr.y,vanishX,vanishY);
    } else {
        vanishX = vanishAttr.x;
        vanishY = vanishAttr.y;
    }
    HI_MESSAGE_S message = {};
    message.what = HI_EVENT_STATEMNG_ADAS_CALIBRATION;
    message.arg1 = vanishX;
    message.arg2 = vanishY;
    ret = PDT_UI_WINMNG_StartWindow(BUSY_PAGE, HI_FALSE);
    HI_APPCOMM_CHECK_RETURN(ret, HI_FAILURE);
    MLOGD("algx:[%d],algy:[%d],vanishX:[%d],vanishY:[%d]\n",algoAttr.x, algoAttr.y, vanishX, vanishY);
    ret = PDT_UI_COMM_SendAsyncMsg(&message, PDT_UI_AdasCalibOnReceiveMsgResult);
    if(ret != HI_SUCCESS) {
        ret = PDT_UI_WINMNG_FinishWindow(BUSY_PAGE);
        HI_APPCOMM_CHECK_RETURN(ret, HI_FAILURE);
        return HI_FAILURE;
    }

    return HI_SUCCESS;
}

static HI_S32 PDT_UI_ADASCalibrateOnClick(HIGV_HANDLE WidgetHdl)
{
    HI_S32 ret;
    if(WidgetHdl == ADAS_CALIBTATE_BACK) {
        ret = PDT_UI_WINMNG_FinishWindow(ADAS_CALIBRATE_WINDOW);
        HI_APPCOMM_CHECK_RETURN_WITH_ERRINFO(ret, HIGV_PROC_GOON, "PDT_UI_WINMNG_FinishWindow");
    }else if(WidgetHdl == ADAS_CALIBTATE_NEXT) {
        ret = PDT_UI_SendADASCalibMsg();
        HI_APPCOMM_CHECK_RETURN(ret, HIGV_PROC_GOON);
    }
    return HI_SUCCESS;
}

HI_S32 PDT_UI_ADASCalibrateOnShow(HIGV_HANDLE WidgetHdl, HI_PARAM wParam, HI_PARAM lParam)
{
    MLOGI("\n");
    return HIGV_PROC_GOON;
}

HI_S32 PDT_UI_ADASCalibrateOnEvent(HIGV_HANDLE WidgetHdl, HI_PARAM wParam, HI_PARAM lParam)
{
    MLOGI("wParam: %#lx, lParam = %#lx\n", (HI_UL)wParam, (HI_UL)lParam);
    return HIGV_PROC_GOON;
}

HI_S32 PDT_UI_ADASCalibrateOnKeyDown(HIGV_HANDLE WidgetHdl, HI_PARAM wParam, HI_PARAM lParam)
{
    HI_S32 ret;
    MLOGD("wParam: %#lx, lParam = %#lx\n", (HI_UL)wParam, (HI_UL)lParam);
    if (wParam == PDT_UI_KEY_BACK) {
        ret = PDT_UI_WINMNG_FinishWindow(ADAS_CALIBRATE_WINDOW);
        HI_APPCOMM_CHECK_RETURN_WITH_ERRINFO(ret, HIGV_PROC_GOON, "PDT_UI_WINMNG_FinishWindow");
    }else if(wParam == PDT_UI_KEY_MENU) {
        if(WidgetHdl == ADAS_CALIBTATE_BACK) {
            ret = HI_GV_Widget_Active(ADAS_CALIBTATE_NEXT);
            HI_APPCOMM_CHECK_RETURN(ret, HIGV_PROC_GOON);
        }else if(WidgetHdl == ADAS_CALIBTATE_NEXT) {
            ret = HI_GV_Widget_Active(ADAS_CALIBTATE_BACK);
            HI_APPCOMM_CHECK_RETURN(ret, HIGV_PROC_GOON);
        }
    }else if(wParam == PDT_UI_KEY_EXPAND) {
        if(WidgetHdl == ADAS_CALIBTATE_BACK) {
            ret = HI_GV_Widget_Active(ADAS_CALIBTATE_NEXT);
            HI_APPCOMM_CHECK_RETURN(ret, HIGV_PROC_GOON);
        }else if(WidgetHdl == ADAS_CALIBTATE_NEXT) {
            ret = HI_GV_Widget_Active(ADAS_CALIBTATE_BACK);
            HI_APPCOMM_CHECK_RETURN(ret, HIGV_PROC_GOON);
        }
    }else if(wParam == PDT_UI_KEY_OK) {
        PDT_UI_ADASCalibrateOnClick(WidgetHdl);
    }

    return HIGV_PROC_GOON;
}

HI_S32 PDT_UI_ADASCalibrateOnGestureTap(HIGV_HANDLE WidgetHdl, HI_PARAM wParam, HI_PARAM lParam)
{
    MLOGD("wParam: %#lx, lParam = %#lx\n", (HI_UL)wParam, (HI_UL)lParam);
    MLOGI("tap:%d\n",WidgetHdl);
    HI_S32 ret = HI_GV_Widget_Active(WidgetHdl);
    HI_APPCOMM_CHECK_RETURN(ret, HIGV_PROC_GOON);
    ret = PDT_UI_ADASCalibrateOnClick(WidgetHdl);
    HI_APPCOMM_CHECK_RETURN(ret, ret);
    return HIGV_PROC_STOP;
}

HI_BOOL PDT_UI_GetAdasCalibStatus(HI_VOID)
{
    return g_adasCalibCtx.calibStatus;
}

HI_VOID PDT_UI_SetAdasCalibStatus(HI_BOOL status)
{
    g_adasCalibCtx.calibStatus = status;
}

#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /*  __cplusplus  */

